package com.sky.controller.admin;

import com.sky.dto.DishPageQueryDTO;
import com.sky.dto.SetmealDTO;
import com.sky.entity.Setmeal;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetMealService;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.Cacheable;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: HuoXu
 * @Date: 2023/07/26/16:24
 * @Description: 快乐跳跳虎
 */
@RestController
@Slf4j
@RequestMapping("/admin/setmeal")
public class SetMealController {

    @Autowired
    private SetMealService setMealService;

    /**
     * 新增套餐
     * @param setmealDTO
     * @return
     */
    @PostMapping
    @CacheEvict(cacheNames = "setmealcache",key = "#setmealDTO.categoryId")
    public Result addPackge(@RequestBody SetmealDTO setmealDTO){
        log.info("进入新增套餐接口,{}",setmealDTO);
        setMealService.addPackge(setmealDTO);
        return Result.success();
    }

    /**
     * 套餐的分页查询
     * @param dishPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    public Result<PageResult> queryByPage(DishPageQueryDTO dishPageQueryDTO){
        log.info("进入套餐的分页查询,{}",dishPageQueryDTO);
        PageResult pageResult=setMealService.queryByPage(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 套餐的起售与停售
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @CacheEvict(cacheNames = "setmealcache",allEntries = true) //此处即删除redis中所有的setmealcache_id的数据
    public Result startOrStop(@PathVariable Integer status,Long id){
        log.info("进入菜品的起售与停售,{},{}",status,id);
        setMealService.startOrStop(status,id);
        return Result.success();
    }

    /**
     * 修改套餐
     * @param setmealDTO
     * @return
     */
    @PutMapping
    @CacheEvict(cacheNames = "setmealcache",allEntries = true) //此处即删除redis中所有的setmealcache_id的数据
    public Result editSetMeal(@RequestBody SetmealDTO setmealDTO){
        log.info("进入修改套餐,{}",setmealDTO);
        setMealService.editSetMeal(setmealDTO);
        return Result.success();
    }

    /**
     * 修改套餐前的数据回显
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result queryById(@PathVariable Long id){
        SetmealVO setmealvo=setMealService.queryById(id);
        return Result.success(setmealvo);
    }

    /**
     * 批量删除套餐
     * @param ids
     * @return
     */
    @DeleteMapping
    @CacheEvict(cacheNames = "setmealcache",allEntries = true) //此处即删除redis中所有的setmealcache_id的数据
    public Result deleteByIds(@RequestParam List<Long> ids){
        log.info("进入批量删除套餐的窗口,{}",ids);
        setMealService.deleteByIds(ids);
        return Result.success();
    }
}
